package com.gexin.service.memberAcitivty;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gexin.common.utils.Pagination;
import com.gexin.model.activity.GeneralIntegralProportionRule;
import com.gexin.model.activity.InviteeRule;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.activerecord.tx.TxConfig;

import java.util.*;

public class MemberRuleService {


    public Pagination findRule() {
        List<InviteeRule> inviteeRuleList = InviteeRule.dao.findAll();
        Pagination pagination = new Pagination();
        pagination.setCode(0);
        pagination.setCount(inviteeRuleList.size());
        pagination.setData(inviteeRuleList);
        pagination.setMsg("返回成功");
        return pagination;
    }

    public Pagination integralList() {
        List<InviteeRule> inviteeRuleList = InviteeRule.dao.findAll();
        Pagination pagination = new Pagination();
        pagination.setMsgAndCountAndData(0, "返回成功", inviteeRuleList.size(), inviteeRuleList);
        return pagination;
    }



    public Boolean insertGeneralIntegralProportionRule(Integer level, Integer proportion) {
        InviteeRule inviteeRule = new InviteeRule();
        inviteeRule.setId(UUID.randomUUID().toString());
        inviteeRule.setLevel(level);
        inviteeRule.setProportion(proportion);
        long time = new Date().getTime();

        inviteeRule.setCreateTime(time);
        inviteeRule.setUpdateTime(time);
        Boolean flag = false;
        flag = inviteeRule.save();
        return flag;
    }


    @Before(Tx.class)
    public Boolean deleteInviteeRule(String[] idList) {
        ArrayList<String> sql_list = new ArrayList<>();
        for(int i=0;i<idList.length;i++){
            String s = idList[i];
            sql_list.add("DELETE FROM t_invitee_rule WHERE id = "+'"'+s+'"'  );
        }
        Db.batch(sql_list,1);
        return true;
    }

    public Boolean updateInviteeRule(Integer integralProportion, String id) {
        InviteeRule inviteeRule = new InviteeRule();
        inviteeRule.setId(id);
        inviteeRule.setProportion(integralProportion);
        boolean flag = inviteeRule.update();
        return flag;
    }
    public InviteeRule checkDuplicateMemberLevel(Integer level) {
        InviteeRule inviteeRule = InviteeRule.dao.findFirst("select * from t_invitee_rule where level=" + level);
        return inviteeRule;
    }

}
